package com.video.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.video.bean.UserInfo;
import com.video.bean.Video;
import com.video.bean.Vip;
import com.video.dao.VideoDao;
import com.video.dao.VipDao;
import com.video.service.VideoServiceI;
import com.video.utils.DateConfig;
import com.video.utils.ResultMap;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class VideoServiceImpl implements VideoServiceI {
    @Autowired
    private VideoDao videoDao;
    @Autowired
    private VipDao vipDao;

    @Override
    public ResultMap selectAllVideo() {
        List<Video> list = videoDao.selectAllVideo();
        return ResultMap.createBySuccess(list);
    }

    @Override
    public ResultMap selectAllVideoByLimit(int page, int limit) {
        PageHelper.startPage(page, limit);//开启分页
        List<Video> list = videoDao.selectAllVideo();
        PageInfo<Video> pageInfo = new PageInfo<Video>(list);
        list = pageInfo.getList();
        return ResultMap.selectLimitBySuccess(0,pageInfo.getTotal(),list);
    }

    @Override
    public ResultMap selectVideoByTid(String tid) {
        List<Video> list = videoDao.selectVideoByTid(tid);
        return ResultMap.createBySuccess(list);
    }

    @Override
    public void createVideo(Video video, HttpServletRequest request) {
        //1、上传文件
        MultipartFile coverFile = video.getCoverFile();//图片
        MultipartFile videoFile = video.getVideoFile();//视频

        String coverName = coverFile.getOriginalFilename(); //获取图片文件原来名称
        String coverNewName = UUID.randomUUID().toString()+coverName.substring(coverName.lastIndexOf(".")); //生成图片新名称

        String videoName = videoFile.getOriginalFilename(); //获取视频文件原来名称
        String videoNewName = UUID.randomUUID().toString()+videoName.substring(videoName.lastIndexOf(".")); //生成视频新名称

        //数据库存储图片地址
        String imgAddress = request.getServletContext().getContextPath()+"/images/"+coverNewName;
        //图片真实上传地址：D:\IdeaProjects\VideoOnline\out\artifacts\VideoOnline_Web_exploded\
        String imgRealAddress = request.getServletContext().getRealPath("/")+"images/"+coverNewName;

        //数据库存储视频地址
        String videoAddress = request.getServletContext().getContextPath()+"/videos/"+videoNewName;
        //视频真实上传地址
        String videoRealAddress = request.getServletContext().getRealPath("/")+"videos/"+videoNewName;

        File targetCover = new File(imgRealAddress);//图片目标文件
        File targetVideo = new File(videoRealAddress);//视频目标文件
        try {
            coverFile.transferTo(targetCover);//上传图片
            videoFile.transferTo(targetVideo);//上传视频
        } catch (Exception e) {
            e.printStackTrace();
        }
        //2、将信息存入数据库
        String vid = UUID.randomUUID().toString();
        video.setVid(vid);
        video.setCoverImgAddress(imgAddress);
        video.setVideoAddress(videoAddress);
        video.setCoverImgName(coverName);
        video.setVideoName(videoName);
        video.setCoverImgNewName(coverNewName);
        video.setVideoNewName(videoNewName);
        videoDao.createVideo(video);
    }

    @Override
    public ResultMap updateVideoByVId(String videoStr) {
        Video video = JSONObject.parseObject(videoStr,Video.class);
        videoDao.updateVideoByVId(video);
        return ResultMap.createBySuccess();
    }

    @Override
    public ResultMap deleteVideoByVid(String vId) {
        //这里要加业务逻辑
        videoDao.deleteVideoByVid(vId);
        return ResultMap.createBySuccess();
    }

    @Override
    public ResultMap selectVideoByVid(String vid, HttpSession session) {
        UserInfo user = (UserInfo) session.getAttribute("user");
        String username = user.getUsername();

        Vip vip = vipDao.selectVipByUsername(username);
        Video video = videoDao.selectVideoByVid(vid);
        if(video.getIsVip() == 0){//会员电影
            if(vip == null){//用户不是会员
                return ResultMap.createByError("您还不是会员，请充值~");
            }else{
                Date startTime = vip.getStartTime();
                Date endTime = vip.getEndTime();
                String startTimeStr = DateConfig.DateToStr(startTime);
                String endTimeStr = DateConfig.DateToStr(endTime);
                long day = DateConfig.equalsDate(startTimeStr,endTimeStr);
                if(day < 0){
                    return ResultMap.createByError("您的会员已到期，请充值~");
                }else{
                    return ResultMap.createBySuccess(video);
                }
            }
        }else{//非会员电影
            return ResultMap.createBySuccess(video);
        }
    }

}
